import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH = 800
HEIGHT = 600
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 100
BALL_RADIUS = 10
SPEED = 5

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("羽毛球游戏")

# 球拍类
class Paddle:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.speed = SPEED

    def move_up(self):
        if self.y > 0:
            self.y -= self.speed

    def move_down(self):
        if self.y < HEIGHT - PADDLE_HEIGHT:
            self.y += self.speed

    def draw(self):
        pygame.draw.rect(screen, WHITE, (self.x, self.y, PADDLE_WIDTH, PADDLE_HEIGHT))

# 羽毛球类
class Ball:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.vx = random.choice([-SPEED, SPEED])
        self.vy = random.randint(-SPEED, SPEED)

    def move(self):
        self.x += self.vx
        self.y += self.vy
        # 边界检测
        if self.y - BALL_RADIUS < 0 or self.y + BALL_RADIUS > HEIGHT:
            self.vy = -self.vy

    def draw(self):
        pygame.draw.circle(screen, RED, (int(self.x), int(self.y)), BALL_RADIUS)

# 创建球拍和羽毛球
left_paddle = Paddle(20, HEIGHT // 2 - PADDLE_HEIGHT // 2)
right_paddle = Paddle(WIDTH - 20 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2)
ball = Ball(WIDTH // 2, HEIGHT // 2)

# 得分
left_score = 0
right_score = 0

# 游戏主循环
clock = pygame.time.Clock()
running = True
while running:
    screen.fill(BLACK)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                left_paddle.move_up()
            elif event.key == pygame.K_s:
                left_paddle.move_down()
            elif event.key == pygame.K_UP:
                right_paddle.move_up()
            elif event.key == pygame.K_DOWN:
                right_paddle.move_down()

    # 羽毛球移动
    ball.move()

    # 碰撞检测
    if (ball.x - BALL_RADIUS <= left_paddle.x + PADDLE_WIDTH and
            left_paddle.y <= ball.y <= left_paddle.y + PADDLE_HEIGHT):
        ball.vx = -ball.vx
    elif (ball.x + BALL_RADIUS >= right_paddle.x and
            right_paddle.y <= ball.y <= right_paddle.y + PADDLE_HEIGHT):
        ball.vx = -ball.vx

    # 得分检测
    if ball.x - BALL_RADIUS < 0:
        right_score += 1
        ball.x = WIDTH // 2
        ball.y = HEIGHT // 2
        ball.vx = random.choice([-SPEED, SPEED])
        ball.vy = random.randint(-SPEED, SPEED)
    elif ball.x + BALL_RADIUS > WIDTH:
        left_score += 1
        ball.x = WIDTH // 2
        ball.y = HEIGHT // 2
        ball.vx = random.choice([-SPEED, SPEED])
        ball.vy = random.randint(-SPEED, SPEED)

    # 绘制球拍和羽毛球
    left_paddle.draw()
    right_paddle.draw()
    ball.draw()

    # 显示得分
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"{left_score} : {right_score}", True, WHITE)
    screen.blit(score_text, (WIDTH // 2 - 20, 10))

    # 更新显示
    pygame.display.flip()
    clock.tick(60)

# 退出游戏
pygame.quit()